<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>运营工具</cite></a>
    <a><cite>批量转售</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_resale_product_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">用户手机号</div>
            <div class="layui-input-inline">
              <input type="text" name="mobile" value="{{decodeURIComponent(layui.router().search.mobile || '')}}"
                     class="layui-input"
                     placeholder="请输入用户手机号">
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">二级密码</div>
            <div class="layui-input-inline">
              <input type="text" name="second_pass"
                     value="{{decodeURIComponent(layui.router().search.second_pass || '')}}"
                     class="layui-input"
                     placeholder="请输入用户账户的二级密码">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_resale_product_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_resale_product_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_resale_product"></div>
      <script type="text/html" id="tpl_user">
        {{d.user.mobile}}
      </script>
      <script type="text/html" id="tpl_product">
        {{d.product.name}}
      </script>

      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link" lay-event="batch_resale">
          批量转售
        </a>
        <div class="ant-divider"></div>
        <a class="layui-table-link layui-font-red" lay-event="show_cancel_list">
          批量取消
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , admin = layui.admin
      , form = layui.form
      , view = layui.view

    let resourceUrl = 'agent-admin/business/user_assets'

    // 列表查询
    fox_table.render({
      id: 'table_resale_product'
      , url: resourceUrl
      , cols: [[
        {title: '商品名称', templet: '#tpl_product', minWidth: 200}
        , {title: '资产数量', field: 'total_count', width: 200, align: "center"}
        , {title: '可转售数量', field: 'hold_count', width: 100, align: "center"}
        , {title: '转售中数量', field: 'onsale_count', width: 100, align: "center"}
        , {title: '操作', width: 200, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      data.mobile = layui.router().search.mobile
      if (obj.event === 'batch_resale') {
        showBatchResaleForm(data)
      }
      if (obj.event === 'show_cancel_list') {
        showTokensForm(data)
      }
    });

    //搜索
    form.on('submit(submit_resale_product_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_resale_product_search)', function (d) {
      $("input[name='mobile']").val('');
      $("input[name='second_pass']").val('');
      fox_table.reload({});
      return false;
    });

    //批量转售弹窗
    function showBatchResaleForm(data) {
      let minWidth = '450px';
      let minHeight = '300px';
      data.mobile = layui.router().search.mobile
      admin.popup({
        title: '批量转售'
        , area: [minWidth, minHeight]
        , id: 'popup_batch_resale'
        , success: function (layero, index) {
          view(this.id).render('business/resale_form', data).done(function () {
            form.on('submit(submit_resale)', function (d) {
              let field = d.field;
              if (field.quantity > 50) {
                layer.msg('最大转售数量为50')
                return false;
              }
              $('#submit_resale').attr('disabled', true).val('请求处理中，请耐心等待');
              admin.post("agent-admin/business/batch_resale", field, function (res) {
                layer.close(index);
                fox_table.reload();
              })
            })
          })
        }
      })
    }

    //批量取消转售弹窗
    function showTokensForm(data) {
      let minWidth = '600px';
      let minHeight = '500px';
      admin.popup({
        title: '转售中的价格和数量'
        , area: [minWidth, minHeight]
        , id: 'popup_resaling'
        , success: function (layero, index) {
          view(this.id).render('business/resaling_form', data).done(function () {
          })
        }
      })
    }


  })
</script>

